package com.pxmeta.party.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.party.entity.TurntableNumber;
import com.pxmeta.party.entity.UserTurntableNumber;
import com.pxmeta.party.mapper.TurntableNumberMapper;
import com.pxmeta.party.mapper.UserTurntableNumberMapper;
import com.pxmeta.party.service.ITurntableNumberService;
import com.pxmeta.party.service.IUserTurntableNumberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;


@Service
@RequiredArgsConstructor
public class UserTurntableNumberServiceImpl extends ServiceImpl<UserTurntableNumberMapper, UserTurntableNumber> implements IUserTurntableNumberService {

    @Override
    public UserTurntableNumber getUserTurntableNumber(Long userId, Integer type) {
        LambdaQueryWrapper<UserTurntableNumber> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserTurntableNumber::getUserId, userId)
                .eq(UserTurntableNumber::getLotteryType, type);

        UserTurntableNumber userTurntableNumber = this.getOne(wrapper);
        if (null == userTurntableNumber){
            UserTurntableNumber newUserTurntableNumber = new UserTurntableNumber();
            newUserTurntableNumber.setUserId(userId);
            newUserTurntableNumber.setGiftNumber(BigDecimal.ZERO);
            newUserTurntableNumber.setConsumeNumber(BigDecimal.ZERO);
            newUserTurntableNumber.setTenLotteryCount(0);
            newUserTurntableNumber.setLotteryCount(0);
            newUserTurntableNumber.setSinglePointCount(0);
            newUserTurntableNumber.setCount(0);
            newUserTurntableNumber.setLotteryType(type);
            this.save(newUserTurntableNumber);
            return newUserTurntableNumber;
        }

        return userTurntableNumber;
    }
}
